using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace Eve.Data
{
#if (!WEB)
    internal class MySQLDatabase : Database
    {
        MySqlConnection conn;

        public MySQLDatabase()
            : base(DatabaseType.MySQL)
        {
        }

        public override bool TableExists(string table)
        {
            return ExecuteScalar("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='Version' AND TABLE_SCHEMA='" + Name + "'");
        }

        protected override IDbConnection OpenConnection(string connectionString)
        {
            if (conn != null && conn.ConnectionString != connectionString)
            {
                conn.Close();
                conn = null;
            }
            if (conn == null) conn = new MySqlConnection(connectionString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken) conn.Open();
            return conn;
        }
        protected override IDbDataAdapter GetDataAdapter(string query, string connectionString)
        {
            return new MySqlDataAdapter(query, (MySqlConnection)OpenConnection(connectionString));
        }
        protected override IDbCommand GetCommand(string query, string connectionString)
        {
            return new MySqlCommand(query, (MySqlConnection)OpenConnection(connectionString));
        }
    }
#endif
}
